package com.xiaoxian.org.service.impl;

import com.xiaoxian.basic.commoneumn.ErrorCode;
import com.xiaoxian.basic.constant.Constant;
import com.xiaoxian.basic.exception.CustomException;
import com.xiaoxian.basic.service.impl.BaseServiceImpl;
import com.xiaoxian.basic.util.PageList;
import com.xiaoxian.org.domain.Department;
import com.xiaoxian.org.mapper.DepartmentMapper;
import com.xiaoxian.org.query.DepartmentQuery;
import com.xiaoxian.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Transactional
    @Override
    public void add(Department department) {
        departmentMapper.add(department);
        this.update(department);
        redisTemplate.delete(Constant.DEPT_TREE_KEY);

    }
    @Transactional
    @Override
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key = "'tree'")
    public void delete(Long id) {
        departmentMapper.delete(id);
       // redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }
    @Transactional
    @Override
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key = "'tree'")
    public void update(Department department) {
        String dirPath="/";
        //
        if(department.getParent()==null){
            dirPath=dirPath+department.getId();
        }else {
            Department one = departmentMapper.findOne(department.getParent().getId());
            dirPath=one.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
        //redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }


    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY,key = "'tree'")
    public void patchDelete(List<Long> ids) {
        departmentMapper.patchDelete(ids);
        //redisTemplate.delete(Constant.DEPT_TREE_KEY);
    }

    @Override
    @Cacheable(cacheNames = Constant.DEPT_TREE_KEY,key = "'tree'")
    public List<Department> deptTree() {
//        List<Department> result = (List<Department>)redisTemplate.opsForValue().get(Constant.DEPT_TREE_KEY);
//        if(result==null){
//            result=getDepartmentsTree();
//            redisTemplate.opsForValue().set(Constant.DEPT_TREE_KEY,result);
//        }
        return getDepartmentsTree();
    }


    public List<Department> getDepartmentsTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        // 2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
        // 3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中
        //将所有的部门撞到map中  - 每次获取父部门都从这里获取
        List<Department> all = departmentMapper.findAll();
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //集合数据，部门树
        List<Department> deptTree = new ArrayList<>();
        for (Department department : all) {
            if(department.getParent_id()==null){
                deptTree.add(department);
            }else {
//                for (Department parent : all) {
//                    if (parent.getId().equals(department.getParent_id())) {
//                        parent.getChildren().add(department);
//
//                    }
//                }
                //1.想办法找到当前部门的 父部门
                    Department dept = map.get(department.getParent_id());
                    //将放入集合数据中
                    dept.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
